import pygame
from pygame.sprite import Sprite

class Athlete(Sprite):
    def __init__(self, screen, ai_settings):
        super(Athlete,self).__init__()
        self.screen = screen
        self.ai_settings = ai_settings
        self.radius = 10

        self.color = (0, 0, 255)
        self.pos = [0, 0]
        self.rect = pygame.rect.Rect((self.pos[0], self.pos[1]), (self.radius, self.radius))
        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False
        self.speedx = 0.7
        self.speedy = 0.7
        self.keep = False  # Confirm whether the athlete has the ball
        self.fireBall_speedx = 1000
        self.fireBall_speedy = 1000

    def updateColidence(self,pos):
        self.rect = pygame.rect.Rect((pos[0], self.pos[1]), (self.radius, self.radius))

    def update(self):
        self.updateColidence(self.pos)
        if self.moving_right:
            self.pos[0] += self.speedx
        if self.moving_left:
            self.pos[0] -= self.speedx
        if self.moving_up:
            self.pos[1] -= self.speedy
        if self.moving_down:
            self.pos[1] += self.speedy




#The default first operating angle of view is the blue center winger
class Center(Athlete):  # Center class
    def __init__(self, screen, ai_settings, color):
        super(Center, self).__init__(screen, ai_settings)
        self.color = color
        if self.color == (0, 0, 255):
            self.pos = [578,336]
        elif self.color == (255, 0, 0):
            self.pos = [623,336]
        self.rect = pygame.rect.Rect((self.pos[0], self.pos[1]), (self.radius, self.radius))
        self.colidence_flagR=False
        self.maxX = 1060
        self.maxY = 610
        self.minX = 130
        self.minY = 80
    #Center can run full court
    def update(self):
        if self.moving_right:
            if self.pos[0]+self.speedx<self.maxX  :
                self.pos[0] += self.speedx
        if self.moving_left:
            if self.pos[0] - self.speedx > self.minX:
                self.pos[0] -= self.speedx
        if self.moving_up:
            if self.pos[1]-self.speedy>self.minY:
                self.pos[1] -= self.speedy
        if self.moving_down:
            if self.pos[1] + self.speedy < self.maxY:
                self.pos[1] += self.speedy
        self.updateColidence(self.pos)

    def restore(self):
        if self.color == (0, 0, 255):
            self.pos = [578, 336]
        elif self.color == (255, 0, 0):
            self.pos = [623, 336]

# goalkeeper
class goaltender(Athlete):
    def __init__(self, screen, ai_settings, color):
        super(goaltender, self).__init__(screen, ai_settings)
        self.color = color
        self.minY=280
        self.maxY=408
        self.speedy=0.7
        # Blue team
        if self.color == (0, 0, 255):
            self.pos = [195, 340]
        # Red team
        elif self.color == (255, 0, 0):
            self.pos = [1004, 340]


    #The goalkeeper is not allowed to move left or right
    def update(self):
        if self.moving_right:
            pass
        if self.moving_left:
            pass
        if self.moving_up:
            if self.pos[1]-self.speedy>self.minY:
                self.pos[1] -= self.speedy
        if self.moving_down:
            if self.pos[1]+self.speedy<self.maxY:
                self.pos[1] += self.speedy

    def restore(self):
        # Blue team
        if self.color == (0, 0, 255):
            self.pos = [195, 340]
        # Red team
        elif self.color == (255, 0, 0):
            self.pos = [1004, 340]

class LDefenseman(Athlete):  # Left back
    def __init__(self, screen, ai_settings, color):
        super(LDefenseman, self).__init__(screen, ai_settings)
        self.color = color
        if self.color == (0, 0, 255):
            self.pos = [442,218]
            self.maxX = 600
            self.maxY = 610
            self.minX = 130
            self.minY = 80
        elif self.color == (255, 0, 0):
            self.pos = [757,218]
            self.maxX = 1060
            self.maxY = 610
            self.minX = 600
            self.minY = 80
    #Point guard halftime
    def update(self):
        if self.moving_right:
            if self.pos[0]+self.speedx<self.maxX:
                self.pos[0] += self.speedx
        if self.moving_left:
            if self.pos[0] - self.speedx > self.minX:
                self.pos[0] -= self.speedx
        if self.moving_up:
            if self.pos[1]-self.speedy>self.minY:
                self.pos[1] -= self.speedy
        if self.moving_down:
            if self.pos[1] + self.speedy < self.maxY:
                self.pos[1] += self.speedy

    def restore(self):
        # Blue team
        if self.color == (0, 0, 255):
            self.pos = [442, 218]
        elif self.color == (255, 0, 0):
            self.pos = [757, 218]

class RDefenseman(Athlete):  # Right back
    def __init__(self, screen, ai_settings, color):
        super(RDefenseman, self).__init__(screen, ai_settings)
        self.color = color
        if self.color == (0, 0, 255):
            self.pos = [442,466]
            self.maxX = 600
            self.maxY = 610
            self.minX = 130
            self.minY = 80
        elif self.color == (255, 0, 0):
            self.pos = [757,466]
            self.maxX = 1068
            self.maxY = 610
            self.minX = 600
            self.minY = 80
    def update(self):
        if self.moving_right:
            if self.pos[0]+self.speedx<self.maxX:
                self.pos[0] += self.speedx
        if self.moving_left:
            if self.pos[0] - self.speedx > self.minX:
                self.pos[0] -= self.speedx
        if self.moving_up:
            if self.pos[1]-self.speedy>self.minY:
                self.pos[1] -= self.speedy
        if self.moving_down:
            if self.pos[1] + self.speedy < self.maxY:
                self.pos[1] += self.speedy

    def restore(self):
        if self.color == (0, 0, 255):
            self.pos = [442,466]
        elif self.color == (255, 0, 0):
            self.pos = [757,466]

class LWinger(Athlete):  # Left winger
    def __init__(self, screen, ai_settings, color):
        super(LWinger, self).__init__(screen, ai_settings)
        self.color = color
        if self.color == (0, 0, 255):
            self.pos = [578,150]
            self.maxX = 1060
            self.maxY = 610
            self.minX = 135
            self.minY = 80
        elif self.color == (255, 0, 0):
            self.pos = [623,150]
            self.maxX = 1060    #It turned out to be 760 halftime
            self.maxY = 610
            self.minX = 135
            self.minY = 80
    def update(self):
        if self.moving_right:
            if self.pos[0]+self.speedx<self.maxX:
                self.pos[0] += self.speedx
        elif self.moving_left:
            if self.pos[0] - self.speedx > self.minX:
                self.pos[0] -= self.speedx
        if self.moving_up:
            if self.pos[1]-self.speedy>self.minY:
                self.pos[1] -= self.speedy
        elif self.moving_down:
            if self.pos[1] + self.speedy < self.maxY:
                self.pos[1] += self.speedy

    def restore(self):
        if self.color == (0, 0, 255):
            self.pos = [578,150]
        elif self.color == (255, 0, 0):
            self.pos = [623,150]

class RWinger(Athlete):  # Right winger
    def __init__(self, screen, ai_settings, color):
        super(RWinger, self).__init__(screen, ai_settings)
        self.color = color
        if self.color == (0, 0, 255):
            self.pos = [578,522]
            self.maxX = 1060
            self.maxY = 610
            self.minX = 130
            self.minY = 80
        elif self.color == (255, 0, 0):
            self.pos = [623,522]
            self.maxX = 1060
            self.maxY = 610
            self.minX = 130
            self.minY = 80
    def update(self):
        if self.moving_right:
            if self.pos[0]+self.speedx<self.maxX:
                self.pos[0] += self.speedx
        if self.moving_left:
            if self.pos[0] - self.speedx > self.minX:
                self.pos[0] -= self.speedx
        if self.moving_up:
            if self.pos[1]-self.speedy>self.minY:
                self.pos[1] -= self.speedy
        if self.moving_down:
            if self.pos[1] + self.speedy < self.maxY:
                self.pos[1] += self.speedy

    def restore(self):
        if self.color == (0, 0, 255):
            self.pos = [578, 522]
        elif self.color == (255, 0, 0):
            self.pos = [623,522]